package com.ruoyi.project.inspection.controller;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.inspection.domain.ViVehicleType;
import com.ruoyi.project.inspection.service.IViVehicleTypeService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车辆类型Controller
 *
 * @author ruoyi
 * @date 2020-07-17
 */
@RestController
@RequestMapping("/inspection/type")
public class ViVehicleTypeController extends BaseController {
    @Autowired
    private IViVehicleTypeService viVehicleTypeService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询车辆类型列表
     */
    @PreAuthorize("@ss.hasPermi('inspection:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(ViVehicleType viVehicleType) {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        viVehicleType.setUserId(user.getUserId());
        List<ViVehicleType> list = viVehicleTypeService.selectViVehicleTypeList(viVehicleType);
        return getDataTable(list);
    }

    /**
     * 导出车辆类型列表
     */
    @PreAuthorize("@ss.hasPermi('inspection:type:export')")
    @Log(title = "车辆类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ViVehicleType viVehicleType) {
        List<ViVehicleType> list = viVehicleTypeService.selectViVehicleTypeList(viVehicleType);
        ExcelUtil<ViVehicleType> util = new ExcelUtil<ViVehicleType>(ViVehicleType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 获取车辆类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('inspection:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(viVehicleTypeService.selectViVehicleTypeById(id));
    }

    /**
     * 新增车辆类型
     */
    @PreAuthorize("@ss.hasPermi('inspection:type:add')")
    @Log(title = "车辆类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ViVehicleType viVehicleType) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        viVehicleType.setUserId(user.getUserId());
        viVehicleType.setComparyId(user.getCompanyId());
        viVehicleType.setCreateId(user.getUserId());
        viVehicleType.setDeptId(user.getDeptId());
        return toAjax(viVehicleTypeService.insertViVehicleType(viVehicleType));
    }

    /**
     * 修改车辆类型
     */
    @PreAuthorize("@ss.hasPermi('inspection:type:edit')")
    @Log(title = "车辆类型", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ViVehicleType viVehicleType) {
        return toAjax(viVehicleTypeService.updateViVehicleType(viVehicleType));
    }

    /**
     * 删除车辆类型
     */
    @PreAuthorize("@ss.hasPermi('inspection:type:remove')")
    @Log(title = "车辆类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(viVehicleTypeService.deleteViVehicleTypeByIds(ids));
    }
}
